import axios from 'axios'

import {baseURL} from '../common/config'

import Vue from 'vue'

let count = 0
export function L_request(config , isLoading = true) {
	if (count === 0 && isLoading) {
    Vue.prototype.$Spin.show()
  }
  count++
	const instance = axios.create({
    // 公用的网络请求路径
    baseURL,
    // 网络请求时间超时会自动断开
		timeout: 5000,
  })
  
  // 发起拦截
	instance.interceptors.request.use(
		(config) => {
			return config
		},
		(err) => {
			console.log(err)
		}
	)
	// 响应拦截
	instance.interceptors.response.use(
		(res) => {
      count--

      if (count === 0) {
			  setTimeout(() => {
          Vue.prototype.$Spin.hide()
        }, 200);
      }
			return res.data
		},
		(err) => {
      console.log(err)
      Vue.prototype.$Spin.hide()
      
		}
	)
	return instance(config)
}
